package framework.dao.imp;

import framework.dao.RecordDao;
import framework.pojo.Record;

import java.util.List;

public class RecordDaoImp extends BaseDao implements RecordDao {


    @Override
    public int AddRecord(Record entry) {
        String sql = "Insert into a_record(uid,id,name,stat) values(?,?,?,?)";
        return update(sql, entry.getUid(), entry.getId(), entry.getName(), entry.isStat());
    }

    @Override
    public List<Record> GetRecordByUidAndStat(Integer uid, Boolean stat, Integer page_id, Integer page_size) {
        String sql;

        if (stat == null && uid == null) {
            sql = "select *from a_record limit ?,?";
            return queryForList(Record.class, sql, (page_id - 1) * page_size, page_size);
        } else if (stat == null) {
            sql = "select uid,id,name,stat from a_record where uid=? limit ?,?";
            return queryForList(Record.class, sql, uid, (page_id - 1) * page_size, page_size);
        } else if (uid == null) {
            sql = "select uid,id,name,stat from a_record where stat=? limit ?,?";
            return queryForList(Record.class, sql, stat, (page_id - 1) * page_size, page_size);
        }
        sql = "select uid,id,name,stat from a_record where uid=? and stat=? limit ?,?";
        return queryForList(Record.class, sql, uid, stat, (page_id - 1) * page_size, page_size);
    }

    @Override
    public Integer GetEntryCount(Integer uid, Boolean stat) {
        String sql;
        Number cnt;
        if (stat == null && uid == null) {
            sql = "select count(*) from a_record";
            cnt = (Number) queryForSingleValue(sql);
            return cnt.intValue();
        } else if (stat == null) {
            System.out.println(uid);
            sql = "select count(*)from a_record where uid=?";
            cnt = (Number) queryForSingleValue(sql, uid);
            return cnt.intValue();
        } else if (uid == null) {
            sql = "select count(*) from a_record where stat=?";
            cnt = (Number) queryForSingleValue(sql, stat);
            return cnt.intValue();
        } else {
            sql = "select count(*) from a_record where uid=? and stat=?";
            cnt = (Number) queryForSingleValue(sql, uid, stat);
            return cnt.intValue();
        }
    }
}
